import { createRouter, createWebHistory } from 'vue-router'
import HomeView from './views/HomeView.vue'
import LoginView from './views/LoginView.vue'
import RegisterView from './views/RegisterView.vue'
import MovieListView from './views/MovieListView.vue'
import MovieDetailView from './views/MovieDetailView.vue'
import AddMovieView from './views/AddMovieView.vue'

const routes = [
  { path: '/', name: 'home', component: HomeView },
  { path: '/login', name: 'login', component: LoginView },
  { path: '/register', name: 'register', component: RegisterView },
  { path: '/movies', name: 'movies', component: MovieListView },
  { 
    path: '/movies/:id', 
    name: 'movie-detail', 
    component: MovieDetailView,
    props: true
  },
  { 
    path: '/add-movie', 
    name: 'add-movie', 
    component: AddMovieView,
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token')
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  } else {
    next()
  }
})

export default router